package com.project.springboot;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.Connection;

/**
 * description: 程序启动入口类
 * @author 朱正磊
 * @date 2023-06-17 15:07:52
 */
@MapperScan({"com.project.springboot.mapper","com.project.springboot.dao"})
@EnableAspectJAutoProxy(exposeProxy = true)
@SpringBootApplication(scanBasePackages = {"com.project.springboot", "com.gitee.yongzhuzl.commonutil"})
@EnableTransactionManagement(proxyTargetClass = true)
@Slf4j
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
        log.info("---------------SpringBoot成功启动！---------------");
    }

	public static void main2(String[] args) throws Exception {
		ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
        DataSource dataSource = context.getBean(DataSource.class);
		System.out.println(dataSource.getClass());

        DruidDataSource druidDataSource = (DruidDataSource) dataSource;
        System.out.println("德鲁伊数据库连接池初始连接数 = " + druidDataSource.getInitialSize()
                + ", 最小连接数 = " + druidDataSource.getMinIdle()
                + ", 最大连接数 = " + druidDataSource.getMaxActive()
                + ", 连接池出借连接的最长期限 = " + druidDataSource.getMaxWait() + "ms");

        Connection connection = dataSource.getConnection();
        // 连接的数据库名称
        System.out.println("连接的数据库名称 = " + connection.getCatalog());
        connection.close();

        // context.getBean(ProductDao.class).addProduct("MP3");
        // context.getBean(ProductDao.class).addProductBatch("TV", "MP3", "MP4");

        ConfigurableEnvironment env = context.getEnvironment();
        String serverPort = env.getProperty("server.port");
        System.out.println("服务端口号 = " + serverPort);

        Environment environment = context.getBean(Environment.class);
        String port = environment.getProperty("server.port");
        String ctxPath = environment.getProperty("server.servlet.context-path");
        ctxPath = ctxPath == null ? "/" : ctxPath;

        log.info("SpringBoot成功启动，运行端口号是：{}，基础访问路径是：{}", port, ctxPath);
    }

}
